package com.gui.wheel;

import com.data.Universe;
import com.data.UniverseManager;
import com.data.Wheel;
import com.gooddies.swing.hComboBox;
import com.gooddies.texteditors.DefaultIntegerTextField;
import com.gooddies.texteditors.DefaultTextField;
import com.swingson.SwingsonGuiBuilder;
import java.awt.Frame;
import javax.swing.JDialog;

/**
 * @author sad
 */
public class WheelEditorDialog extends JDialog {

    private final Wheel wheel;
    private final DefaultIntegerTextField idTF = new DefaultIntegerTextField(0, 1000);
    private final DefaultIntegerTextField channelTF = new DefaultIntegerTextField(508, 512);
    private final DefaultTextField nameTF = new DefaultTextField();
    private final DefaultIntegerTextField xTF = new DefaultIntegerTextField();
    private final DefaultIntegerTextField yTF = new DefaultIntegerTextField();
    private final hComboBox<Universe> universeCB = new hComboBox<Universe>();

    public WheelEditorDialog(Frame frame, Wheel wheel) {
        super(frame, true);
        setTitle("Wheel editor");
        setResizable(false);
        this.wheel = wheel;
        for (Universe universe : UniverseManager.getInstance().iterator()) {
            universeCB.addItem(universe);
        }

        showWheel(wheel);
        SwingsonGuiBuilder.createGuiFromJsonInPackage(this);
    }

    private void showWheel(Wheel wheel) {
        idTF.setValue(wheel.getId());
        channelTF.setValue(wheel.getChannel());
        nameTF.setText(wheel.getWheelName());
        xTF.setValue(wheel.getX());
        yTF.setValue(wheel.getY());
        if (wheel.getUniverse() != null) {
            universeCB.setSelectedItem(wheel.getUniverse());
        }
    }

    private void onSave() {
        if (nameTF.getText().trim().isEmpty()) {
            nameTF.showError("Please enter wheel name");
            return;
        }

        wheel.setId(idTF.getValue());
        wheel.setChannel(channelTF.getValue());
        wheel.setUniverse(universeCB.getSelectedItem());
        wheel.setWheelName(nameTF.getText());
        wheel.setX(xTF.getValue());
        wheel.setY(yTF.getValue());
        setVisible(false);
        dispose();
    }

    private void onCancel() {
        setVisible(false);
        dispose();
    }
}
